using UnityEngine;
using System.Collections;

public class SelectUnitButton : MonoBehaviour {

	public UnitInfo info = new UnitInfo(0, 0, 0);
	public UIAtlas WaitRoomAtlas;
	public UIAtlas UnitAtlas;

	public UITexture unitImage;
	public UILabel unitName;
	public UILabel unitLevel;
	public UILabel unitGrade;

	public UISprite starImage;

    bool isSelected = false;

	void OnClick()
	{
		Debug.Log("My Class is --- " + info.UnitClassID);
		if( CSceneManager.m_Scene != SCENE.SelectUnitScene ) 
		{
			MyInfo.Instance.LastSelectedUnitID = info.UnitClassID;
			MyInfo.Instance.LastSelectedUnitDBID = info.UnitDBID;
			
			CSceneManager c = GameObject.FindObjectOfType(typeof(CSceneManager)) as CSceneManager;
			if( CSceneManager.prevSceneIndex == SCENE.UnitLevelupScene )
				c.GoToUnitLevelUpScene();
			else if(CSceneManager.prevSceneIndex == SCENE.UnitEvolutionScene )
				c.GoToUnitPowerUpScene();
			else
				c.GoToUnitChangeConfirmScene();
		}
		else
		{
			UISprite[] sprites = gameObject.transform.GetComponentsInChildren<UISprite>();
			if( isSelected )
			{
                isSelected = false;
				MyInfo.Instance.LastSelectedUnitIDs.Remove(info.UnitClassID);
				MyInfo.Instance.LastSelectedUnitIDs.Remove(info.UnitDBID);
				foreach(UISprite sprite in sprites)
					sprite.alpha = .5f;

				unitImage.alpha = 1f;
//				gameObject.transform.FindChild("Border").GetComponent<UISprite>().atlas.replacement = WaitRoomAtlas;
//				gameObject.transform.FindChild("Border").GetComponent<UISprite>().spriteName = "sub_frame";
			}
			else
			{
                isSelected = true;
				MyInfo.Instance.LastSelectedUnitIDs.Add(info.UnitClassID);
				MyInfo.Instance.LastSelectedUnitDBIDs.Add(info.UnitDBID);
				foreach(UISprite sprite in sprites)
					sprite.alpha = 1f;

				unitImage.alpha = .5f;
//				gameObject.transform.FindChild("Border").GetComponent<UISprite>().atlas.replacement = WaitRoomAtlas;
//				gameObject.transform.FindChild("Border").GetComponent<UISprite>().spriteName = "select_frame";
			}
		}
	}
}
